package com.moyu.article.servcie.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.util.StringUtil;
import com.moyu.article.domain.Article;
import com.moyu.article.domain.dto.ArticleSaveDTO;
import com.moyu.article.mapper.ArticleMapper;
import com.moyu.article.servcie.IArticleService;
import com.moyu.common.core.enums.ResultCode;
import com.moyu.common.security.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 文章服务层
 *
 * @Author 赵彦博
 * @Date 2025/1/6 9:56
 */
@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {


    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public int create(ArticleSaveDTO articleSaveDTO) {
        StpUtil.checkLogin();
        Long userId = (Long) StpUtil.getLoginId();
        int res = articleMapper.queryStateByUserId(userId);
        if (res == 1) {
            throw new ServiceException(ResultCode.BANNED_USER);
        }
        if (articleSaveDTO.getBoardId() == null
                || StringUtil.isEmpty(articleSaveDTO.getTitle())
                || StringUtil.isEmpty(articleSaveDTO.getContent())) {
            throw new ServiceException(ResultCode.FAILED_ARTICLE_VALUE);
        }
        Article article = new Article();
        article.setBoardId(articleSaveDTO.getBoardId());
        article.setUserId(userId);
        article.setTitle(articleSaveDTO.getTitle());
        article.setContent(articleSaveDTO.getContent());
        article.setState((byte) 0);
        try {
            return articleMapper.insert(article);
        } catch (Exception e) {
            log.error("执行sql-新增帖子出错:" + e.getCause());
        }
        return 0;
    }
}
